package challengeGUI;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import scaler.Scaler;
import challengesManager.ChallengeManager;

/**
 * In this class everything concerning the graphic representation of the this challenge is implemented.
 * This class is a special kind of JPanel which will be integrated into the larger GUI of the gaming screen.
 * @author Yuliy
 *
 */
@SuppressWarnings("serial")
public abstract class ChallengeGUI extends JPanel{
	public static final int DEFAULT_UPDATE = 4000;
	
	public final ImageIcon right;
	public final ImageIcon wrong;

	protected ChallengeManager manager;
	protected Scaler scaler;
	protected Dimension myDim;
	
	/**
	 * Constructor. Here all the initialization takes place.
	 * @param manager the control unit for this challenge.
	 * The methods manager.perfectBuzzMoment() and manager.animationOver()
	 * need to be called from inside this class.
	 */
	public ChallengeGUI(ChallengeManager manager){
		this.manager = manager;
		
		myDim = new Dimension(640, 360);
		scaler = new Scaler(myDim, this);
		
		right = new ImageIcon(this.getClass().getResource("/res/Check-icon.png"));
		wrong = new ImageIcon(this.getClass().getResource("/res/delete-icon.png"));
		
		this.setBackground(Color.WHITE);
	}

	/**
	 * This will start a new round of animation for this challenge.
	 * It should reset all values and initiate the next animation. 
	 */
	public abstract void newRound();
	
	/**
	 * Freezes the animation at its current state. Is used to pause the animation
	 * after a player action or a forced break from outside. A reset should NOT occur.
	 * @see unfreeze
	 */
	public abstract void freeze();
//	public void saveFreeze() {
//		freeze();
//		// Ensures that the animator is freezed
//		try {
//			Thread.sleep(30);
//		} catch (InterruptedException e1) {
//			// Shouldn't happen
//		}
//	}
	
	/**
	 * Resumes the animation from a frozen state.
	 * @see freeze
	 */
	public abstract void unfreeze();
	
	/**
	 * This is mainly used to show a player who has buzzed if he or she is correct.
	 * Obscured elements should be revealed. Important elements should be highlighted.
	 * Distracting elements should be removed.
	 * <br>Should be called in frozen state only.
	 * @param info
	 */
	public abstract void reveal(int info);
	
	/**
	 * Will cause the GUI to end all running Threads properly and to free resources.
	 */
	public abstract void end();
	
	protected void paintRightOrWrong(Graphics g, int x, int y, boolean correct) {
		ImageIcon img;
		if(correct) {
			img = right;
		} else {
			img = wrong;
		}
		
		img.paintIcon(this, g, x - img.getIconWidth()/2, y - img.getIconHeight()/2);
	}
}
